<?php
namespace app\admin\controller;
//导入Controller
use think\Controller;
//导入Db
use think\Db;
//导入session
use think\Session;
class Notice extends Allow
{
	//列表
    public function getindex()
    {
       $notice=Db::table("notice")->select();
       foreach($notice as $k=>$v){
            $notice[$k]['begintime']=date("Y-m-d H:i:s",$v['begintime']);
            $notice[$k]['endtime']=date("Y-m-d H:i:s",$v['endtime']);
       };
       // echo '<pre>';
       // var_dump($notice);exit;
       return $this->fetch("Notice/index",['notice'=>$notice]);
    }
    //添加
    public function getadd(){
      //加载公告模板
        return $this->fetch("Notice/add");
    }

    //执行添加
    public function postdoadd(){
        $request=request();
        $data=$request->param();
      //匹配时间是否符合格式
        $preg="/^[1-9]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])\s+(20|21|22|23|[0-1]\d):[0-5]\d:[0-5]\d$/";  
        if(preg_match($preg,$data['begintime'],$arr1)&&preg_match($preg,$data['endtime'],$arr2)){
          //执行添加
            $data["begintime"]=strtotime("{$data['begintime']}");
            $data["endtime"]=strtotime("{$data['endtime']}");
            if($data["begintime"]>$data["endtime"]){$this->error("开始时间不能大于结束时间,请正确填写,亲");}
            unset($data["action"]);
            if($data['title']&&$data['content']){
              if(Db::table("notice")->insert($data)){
                $this->success("公告添加成功","/adminnotice/index");
              }else{
                $this->error("公告添加失败,请重新添加","/adminnotice/add");
              }
            }else{$this->error("标题和内容不能为空,请重新添加");}
            // echo '<pre>';
            // var_dump($data);
        }else{
          $this->error("添加时间格式不符","/adminnotice/add");
        }
    }

    public function getdelete(){
      $request=request();
      $id=$request->param('id');
      //获取content内容,删除已上传图片
      //<p>23<img src="/ueditor/php/upload/image/20180521/1526907971.jpg" title="1526907971.jpg" alt="1526907971.jpg" width="100" height="100"/></p>;
      $data=Db::table("notice")->where("id","{$id}")->select();
      $content=$data[0]['content'];
      //获取图片所在路径
      preg_match_all('/<img.*?src="(.*?)".*?>/is',$content,$array);
      if(Db::table("notice")->where("id","{$id}")->delete()){
        //删除图片资源
        foreach($array[1] as $v){
          unlink('.'.$v);
        };
        $this->success("公告删除成功","/adminnotice/index");
      }else{
        $this->error("公告删除失败");
      }
    }
    public function getedit(){
      $request=request();
      $id=$request->param('id');
      $data=Db::table("notice")->where("id","{$id}")->select();
      $data[0]["begintime"]=date("Y-m-d H:i:s",$data[0]['begintime']);
      $data[0]["endtime"]=date("Y-m-d H:i:s",$data[0]['endtime']);
      $src=$data[0]["content"];
      preg_match_all('/<img.*?src="(.*?)".*?>/is',$src,$array);
      // var_dump($array);
      // var_dump($src);exit;
      Session::set("src",$src);
      return $this->fetch("/Notice/edit",["data"=>$data]);
    }
    public function postdoedit(){
      $request=request();
       //获取编辑之前content里的值,编辑成功则删除删除之前存入的图片
      $src=Session::get("src");
      //获取图片所在路径
      preg_match_all('/<img.*?src="[^http](.*?)".*?>/is',$src,$array);
      // var_dump($array);exit;
      if($array){
                // 删除修改之前的图片资源
                  foreach($array[1] as $v){
                    unlink('./'.$v);
                  }
                }
      // var_dump($array);exit;
      $id=$request->param("id");
      $data=$request->only(["title","begintime","endtime","content"]);
      //匹配时间是否符合格式
        $preg="/^[1-9]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])\s+(20|21|22|23|[0-1]\d):[0-5]\d:[0-5]\d$/";  
        if(preg_match($preg,$data['begintime'],$arr1)&&preg_match($preg,$data['endtime'],$arr2)){
      //执行编辑
            $data["begintime"]=strtotime("{$data['begintime']}");
            $data["endtime"]=strtotime("{$data['endtime']}");
            if($data["begintime"]>$data["endtime"]){$this->error("开始时间不能大于结束时间,请正确填写,亲");}
            if($data['title']&&$data['content']){
              if(Db::table("notice")->where("id","{$id}")->update($data)){
                $this->success("公告修改成功","/adminnotice/index");
              }else{
                $this->error("公告修改失败");
              };
             }else{$this->error("标题和内容不能为空,请重新编辑");}
        }else{
          $this->error("修改的时间格式不符");
        }
    }

    public function getshow(){
       $request=request();
       $id=$request->param("id");
       $notice=Db::table("notice")->where("id","{$id}")->select();
       return $this->fetch("Notice/show",['notice'=>$notice]);
    }
       
}
